<?php

class SQLiteDB extends SQLite3 {
    function __construct(){

        try {
            $this->open(dirname(__FILE__).'/sqlite_ecloud.db');
        }catch (Exception $e){
            die($e->getMessage());
        }

    }
}

class DBUtils {

    private static $db;

    private static function instance(){
        if (!self::$db) {
            self::$db = new SQLiteDB();
        }
    }

    /**
     * 创建表
     * @param string $sql
     */
    public static function create($sql){
        self::instance();
        $result = @self::$db->query($sql);
        if ($result) {
            return true;
        }else{
            echo self::$db->lastErrorMsg();
            exit('create error');
        }
        return false;
    }

    /**
     * 执行增删改操作
     * @param string $sql
     */
    public static function execute($sql){
        self::instance();
        $result = @self::$db->exec($sql);
        if ($result) {
            return true;
        }else{
            echo self::$db->lastErrorMsg();
            exit('error');
        }
        return false;
    }

    /**
     * 获取记录条数
     * @param string $sql
     * @return int
     */
    public static function count($sql){
        self::instance();
        $result = @self::$db->querySingle($sql);
        return $result ? $result : 0;
    }

    /**
     * 查询单个字段
     * @param string $sql
     * @return void|string
     */
    public static function querySingle($sql){
        self::instance();
        $result = @self::$db->querySingle($sql);
        return $result ? $result : '';
    }

    /**
     * 查询单条记录
     * @param string $sql
     * @return array
     */
    public static function queryRow($sql){
        self::instance();
        $result = @self::$db->querySingle($sql,true);
        return $result;
    }

    /**
     * 查询多条记录
     * @param string $sql
     * @return array
     */
    public static function queryList($sql){
        self::instance();
        $result = array();
        $ret = @self::$db->query($sql);
        if (!$ret) {
            return $result;
        }
        while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
            array_push($result, $row);
        }
        return $result;
    }

    public static function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }

    public static function microtime(){
        $mtime=explode(' ',microtime());
        return $mtime[1]+$mtime[0];
    }

}

$tableSql = <<<EOF
      CREATE TABLE TEST
      (
      ID INT PRIMARY KEY     NOT NULL,
      NAME           TEXT    NOT NULL
      );
EOF;

DBUtils::create($tableSql);

$t1 = DBUtils::microtime();

for($i = 0;$i<200;$i++) {
    DBUtils::execute(
        sprintf(
            "insert into test (ID,NAME) values (%s,'%s')", $i, DBUtils::getRandChar(6)
        )
    );
}

$t2 = DBUtils::microtime();

var_dump($t2-$t1);

?>